import userService from "../service/user.service.js";
import jwt from "jsonwebtoken";
import { JWT_SECRET } from "../constant/default.js";

class UserController {
  async register(ctx, next) {
    console.log(ctx.request.body);
    let { user_name, password, isAdmain } = ctx.request.body;
    const res = await userService.createUser(user_name, password, isAdmain);

    ctx.body = {
      code: 0,
      message: "success",
      result: {
        id: res.id,
        name: res.user_name,
      },
    };
  }

  async login(ctx, next) {
    console.log(ctx.request.body);
    let { user_name } = ctx.request.body;

    const { password, ...res } = await userService.getUerInfo({ user_name });

    ctx.body = {
      code: 0,
      message: "login success",
      result: {
        token: jwt.sign(res, JWT_SECRET, { expiresIn: 10 }),
      },
    };
  }
}

export default new UserController();
